package com.service.impl;

import com.dao.AppointDAO;
import com.dao.MedicalResultDAO;
import com.dao.UserDAO;
import com.model._do.AppointmentDO;
import com.model._do.EquipmentDO;
import com.model._do.MedicalResultsDO;
import com.model.dto.ResultRecordDTO;
import com.service.MedicalResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service("medicalResultService")
public class MedicalResultServiceImpl implements MedicalResultService {

    @Autowired
    private MedicalResultDAO medicalResultDAO;

    @Autowired
    private AppointDAO appointDAO;

    @Autowired
    private UserDAO userDAO;

    @Override
    public void saveMedicalResult(MedicalResultsDO medicalResults) {
        medicalResultDAO.pushMedicalRecord(medicalResults);
    }

    @Override
    public MedicalResultsDO getByAppoint(AppointmentDO appoint){
        return medicalResultDAO.getMedicalByAppoint(appoint);
    }

    @Override
    public List<ResultRecordDTO> getByEquipment(EquipmentDO equip) {
        List<AppointmentDO> appList = appointDAO.getAppointPast(equip.getEquipId());
        return appList.stream().map(a -> this.tranToDTO(a,equip.getEquipId())).filter(Objects::nonNull).collect(Collectors.toList());
    }

    private ResultRecordDTO tranToDTO(AppointmentDO appoint, String equipId){
        ResultRecordDTO resultRecord = new ResultRecordDTO();
        resultRecord.setAppoint(appoint);

        resultRecord.setMedical(getByAppoint(appoint));
        String userId = appoint.getDoAppointEr().equals(equipId)? appoint.getAppointEd(): appoint.getDoAppointEr();
        resultRecord.setDoctor(userDAO.getUserByUserId(userId));
        return resultRecord;
    }
}
